/**
 * Created by wenguangjia on 2017/8/31.
 */
(function () {
    'use strict';

    angular
        .module('logcenterApp')
        .controller('esClusterConfigController', esClusterConfigController);
    esClusterConfigController.$inject = ['$scope', 'searchService', 'toaster'];
    function esClusterConfigController($scope, searchService, toaster) {
        var notify = function (type, title, message) {
            toaster.pop(type, title, message);
        };

        /**
         *grid
         */
        var columns = [
            // {field: "id", title: "主键", hidden:true},
            {field: "name", title: "集群名"},
            {field: "statusAddr", title: "集群状态地址"},
            {field: "indexAddr", title: "集群索引地址"}
        ];

        $scope.gridFormOptions = {
            selectable: "row",
            sortable: true,
            resizable: true,
            columnMenu: true,
            toolbar: [
                "create",
                {
                    name: "customEdit",
                    text: "编辑",
                    imageClass: "k-edit",
                    className: "k-custom-edit",
                    iconClass: "k-icon"
                },
                {
                    name: "customDestroy",
                    text: "删除",
                    imageClass: "k-delete",
                    className: "k-custom-delete",
                    iconClass: "k-icon"
                }
            ],
            editable: "popup",
            columns: columns
        };
        $scope.UserModel = kendo.data.Model.define({
            id: "id",
            fields: {
                name: {validation: {required: true}},
                statusAddr: {validation: {required: true}}
            }
        });
        $scope.gridFormOptions.dataSource = new kendo.data.DataSource({
            transport: {
                read: function (e) {
                    searchService.queryCluster().then(
                        function(resp){
                            e.success(resp.data);
                        },function(err){
                            notify('success', '成功', '查询失败'+err);
                            e.error(err);
                        }
                    )
                },
                create: function (e) {
                    //var model = new $scope.UserModel();
                    var model=e.data;
                    searchService.saveCluster(model).then(function (resp) {
                        e.success(resp.data);
                        notify('success', '成功', '新增成功！');
                    }, function (error) {
                        e.error(error);
                        notify('error', '失败', error);

                    });
                },
                update: function (e) {
                    var model=e.data;
                    searchService.saveCluster(model).then(function (resp) {
                        notify('success', '成功', '修改成功！');
                        e.success(resp.data);
                    }, function (error) {
                        e.error(error);
                        notify('error', '失败', error);
                    });
                },
                destroy: function (e) {
                    var model=e.data;
                    searchService.deleteCluster(model.id).then(function (resp) {
                        notify('success', '成功', '删除成功！');
                        e.success();
                    }, function (error) {
                        e.error(error);
                        notify('error', '失败', error);
                    });
                },
                parameterMap: function (options, operation) {
                    if (operation !== "read" && options.models) {
                        return {models: kendo.stringify(options.models)};
                    }
                }
            },
            batch: false,
            schema: {
                model: $scope.UserModel
            }
        });



        $scope.$on("kendoWidgetCreated", function (event, widget) {
            if (widget === $scope.gridForm) {
                widget.element.find(".k-custom-edit").on("click", function (e) {
                    e.preventDefault();
                    var selected = $scope.gridForm.select();
                    if (selected.length == 0) {
                        //alert('No record selected')
                        notify('waring', '提示', '请先选择一行！');
                    } else {
                        $scope.gridForm.editRow(selected);
                    }

                });
                widget.element.find(".k-custom-delete").on("click", function (e) {
                    e.preventDefault();
                    var selected = $scope.gridForm.select();
                    if (selected.length == 0) {
                        //alert('No record selected')
                        notify('waring', '提示', '请先选择一行！');
                    } else {
                        $scope.gridForm.removeRow(selected);
                    }

                });
            }
        });


    }


})();
